package com.zjw.core.algorithm;

public class MaxSubsequenceSum {

    public static void main(String[] args) {
        int[] nums = new int[]{-1,-2,1,6,4,1,17,-3};
        int step = 1;//参与求和的元素个数
        int start = 0;
        int end = 0;
        int maxSum = nums[0];
        while(step < nums.length){
            for (int i = 0; i < nums.length - step; i++) {
                int stepSum = nums[i];

                for (int j = i+1; j < i + step; j++) {
                     stepSum = stepSum + nums[j];
                }
                if(stepSum > maxSum){
                    maxSum = stepSum;
                    end = i + step - 1;
                    start = i;
                }
            }
            step ++;
        }
        System.out.println("最大和:"+maxSum+",开始位置"+(start + 1)+",开始值"+nums[start] + ",结束位置"+(end + 1)+"结束值:"+nums[end]);
    }
}
